<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/9/24
 * Time: 下午5:18
 */

class WxwCarController extends BaseController {
    const GET_CAR_BRAND_NORMAL_TYPE = 1; //获取标准品牌
    const GET_CAR_BRAND_HOT_TYPE = 2;  //获取热门品牌
    const GET_CAR_BRAND_ALL_TYPE = 3;  //获取热门品牌 ＋ 标准品牌

    /*
     * test
     * */
    public function test(){
        $validator = Validator::make(Input::all(),
            array(
                'phone' => 'required',
//                'area' => 'required',
                'check_code' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
            $phone = Input::get('phone');

        } while (false);
        Log::info("info", array("system" => "wxw" ,"event" => "test", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 车型的变速箱
     * */
    public function model_gear_box(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
                'series_id' => 'required',
                'model_year' => 'required',
                'model_gas_rate' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $series_id = Input::get('series_id');
            $model_year = Input::get('model_year');
            $model_gas_rate = Input::get('model_gas_rate');
            $callback = Input::get('callback');

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //车型的年份、排量、变速箱
            $model_gear_box_array = CarStandardInfo::where('series_name_id',$series_id)
                ->where('model_year',$model_year)
                ->where('model_gas_rate',$model_gas_rate)
                ->select(array('model_gear_box as name'))
                ->distinct()->get()->toArray();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array(
                'model_gear_box' => $model_gear_box_array,
            ));
        } while (false);

        if(is_null($callback)){
            $response = json_encode($response);
        }else{
            $response = $callback . "('" . json_encode($response) . "')";
        }
        Log::info("info", array("system" => "wxw" ,"event" => "model_gear_box", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /*
     * 车型的排量查询
     * */
    public function model_gas_rate(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
                'series_id' => 'required',
                'model_year' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $series_id = Input::get('series_id');
            $model_year = Input::get('model_year');
            $callback = Input::get('callback');

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //车型的年份、排量、变速箱
            $model_gas_rate_array = CarStandardInfo::where('series_name_id',$series_id)
                ->where('model_year',$model_year)
                ->select(array('model_gas_rate as name'))
                ->distinct()->get()->toArray();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array(
                'model_gas_rate' => $model_gas_rate_array,
            ));
        } while (false);

        if(is_null($callback)){
            $response = json_encode($response);
        }else{
            $response = $callback . "('" . json_encode($response) . "')";
        }
        Log::info("info", array("system" => "wxw" ,"event" => "model_gas_rate", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /*
     * 车型年款查询
     * */
    public function model_year(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
                'series_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $series_id = Input::get('series_id');
            $callback = Input::get('callback');

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //车型的年份、排量、变速箱
            $model_year_array = CarStandardInfo::where('series_name_id',$series_id)->select(array('model_year as name'))->distinct()->get()->toArray();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array(
                'model_year' => $model_year_array,
            ));
        } while (false);

        if(is_null($callback)){
            $response = json_encode($response);
        }else{
            $response = $callback . "('" . json_encode($response) . "')";
        }
        Log::info("info", array("system" => "wxw" ,"event" => "model_year", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /*
     * 车型的年份、排量、变速箱
     * */
    public function model_select(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
                'series_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $series_id = Input::get('series_id');
            $callback = Input::get('callback');

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //车型的年份、排量、变速箱
            $model_year_array = CarStandardInfo::where('series_name_id',$series_id)->select(array('model_year as name'))->distinct()->get()->toArray();
            $model_gear_box_array = CarStandardInfo::where('series_name_id',$series_id)->select(array('model_gear_box as name'))->distinct()->get()->toArray();
            $model_gas_rate_array = CarStandardInfo::where('series_name_id',$series_id)->select(array('model_gas_rate as name'))->distinct()->get()->toArray();

//            $model_year = array();
//            foreach($model_year_array as $_year){
//                array_push($model_year,$_year['model_year']);
//            }
//
//            $model_gear_box = array();
//            foreach($model_gear_box_array as $_gear){
//                array_push($model_gear_box,$_gear['model_gear_box']);
//            }
//
//            $model_gas_rate = array();
//            foreach($model_gas_rate_array as $_gas){
//                array_push($model_gas_rate,$_gas['model_gas_rate']);
//            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array(
                'model_year' => $model_year_array,
                'model_gear_box' => $model_gear_box_array,
                'model_gas_rate' => $model_gas_rate_array,
            ));
        } while (false);

        if(is_null($callback)){
            $response = json_encode($response);
        }else{
            $response = $callback . "('" . json_encode($response) . "')";
        }
        Log::info("info", array("system" => "wxw" ,"event" => "model_select", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /*
     * 车辆系列
     * */
    public function series(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
                'brand_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $brand_id = Input::get('brand_id');
            $callback = Input::get('callback');

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //获取车辆系列
            $car_series = CarSeriesType::where('car_brand_id',$brand_id)
                ->where('status',1)
                ->select(array('id','name','icon'))
                ->orderBy('sort', 'asc')
                ->get()->toArray();
            $count = count($car_series);
            $result = array(
                'count' => $count,
                'series' => $car_series
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);

        if(is_null($callback)){
            $response = json_encode($response);
        }else{
            $response = $callback . "('" . json_encode($response) . "')";
        }
        Log::info("info", array("system" => "wxw" ,"event" => "series", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /*
     * 车辆品牌
     * */
    public function brands(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_token_args_error_1005");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $type = Input::get('type')?Input::get('type'):WxwCarController::GET_CAR_BRAND_ALL_TYPE;
            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //获取热门品牌数据
            $hot_cars = DataCarBrand::where('is_hot',1)
                ->where('status',1)
                ->select(array('id','initial','name','icon'))
                ->orderBy('sort', 'asc')
                ->get()->toArray();
            //获取标准品牌数据
            $normal_cars = DataCarBrand::where('status',1)
                ->select(array('id','initial','name','icon'))
                ->orderBy('sort', 'asc')
                ->get()->toArray();

            if($type == WxwCarController::GET_CAR_BRAND_NORMAL_TYPE){
                $result = array(
                    'normal_cars' => $normal_cars
                );
            }elseif($type == WxwCarController::GET_CAR_BRAND_HOT_TYPE){
                $result = array(
                    'hot_cars' => $hot_cars
                );
            }else{
                $result = array(
                    'hot_cars' => $hot_cars,
                    'normal_cars' => $normal_cars
                );
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);

        } while (false);
        Log::info("info", array("system" => "wxw" ,"event" => "brands", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}